﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using ByrForumAPI.MetaData;


namespace ByrForumAPI.Json
{
    public class UserJsonConvert : JsonConverter
    {
        public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
        {
            serializer.Serialize(writer, value);
        }

        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            try
            {
                object value = serializer.Deserialize(reader, objectType);
                return value;
            }
            catch (Exception)
            {
                object value = serializer.Deserialize(reader, typeof(System.String));
                User user = new User();
                user.ID = value.ToString();
                return user;
            }   
        }


        /// <summary>
        /// 是否允许转换
        /// </summary>
        public override bool CanConvert(Type objectType)
        {
            bool canConvert = false;
            switch (objectType.FullName)
            {
                case "ByrForumAPI.MetaData.User":
                    canConvert = true;
                    break;
                case "System.String":
                    canConvert = true;
                    break;
            }
            return canConvert;
        }
      
 
    }
}
